package com.beeantec.jnrerc.modules.news.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.beeantec.jnrerc.core.annon.LogInject;
import com.beeantec.jnrerc.core.api.ApiRest;
import com.beeantec.jnrerc.core.api.controller.BaseController;
import com.beeantec.jnrerc.core.api.dto.PagingReqDTO;
import com.beeantec.jnrerc.core.enums.DynamicEnum;
import com.beeantec.jnrerc.dto.request.NewsDelReqDTO;
import com.beeantec.jnrerc.entity.Course;
import com.beeantec.jnrerc.entity.MemberDynamic;
import com.beeantec.jnrerc.entity.News;
import com.beeantec.jnrerc.modules.member.service.DynamicService;
import com.beeantec.jnrerc.modules.news.service.NewsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;

@Api(tags={"新闻"})
@RestController
@RequestMapping("/api/news")
public class NewsController extends BaseController {

    @Autowired
    NewsService newsService;
    @Autowired
    DynamicService dynamicService;

    /**
     * 获取新闻资讯列表
     * @return
     */
    @LogInject(title = "获取新闻资讯列表")
    @CrossOrigin
    @ApiOperation(value = "获取新闻资讯列表")
    @RequestMapping(value = "/list", method = {RequestMethod.POST})
    public ApiRest<IPage<News>> list(@RequestBody PagingReqDTO<News> reqDTO){

        //分页查询并转换
        QueryWrapper<News> courseDTOQueryWrapper = new QueryWrapper<>();
        courseDTOQueryWrapper.lambda().orderByDesc(News::getCreateTime);
        Page<News> page = new Page<>(reqDTO.getCurrent(), reqDTO.getSize());
        IPage<News> newsIpage= newsService.getBaseMapper().selectPage(page,courseDTOQueryWrapper);
        return super.success(newsIpage);
    }


    /**
     * 获取新闻资讯列表
     * @return
     */
    @LogInject(title = "根据标题关键字列表")
    @CrossOrigin
    @ApiOperation(value = "根据标题关键字列表")
    @RequestMapping(value = "/search", method = {RequestMethod.GET})
    public ApiRest<IPage<News>> search(@RequestParam("keyWord") String keyWord,@RequestParam("pageNum") Integer pageNum,@RequestParam("pageSize") Integer pageSize){
        //分页查询并转换
        QueryWrapper<News> courseDTOQueryWrapper = new QueryWrapper<>();
        courseDTOQueryWrapper.lambda().like(News::getTitle,keyWord).orderByDesc(News::getCreateTime);
        Page<News> page = new Page<>(pageNum, pageSize);
        IPage<News> newsIpage= newsService.getBaseMapper().selectPage(page,courseDTOQueryWrapper);
        return super.success(newsIpage);
    }




    /**
     * 根据id获取资讯详情
     * @return
     */
    @LogInject(title = "获取新闻资讯详情")
    @CrossOrigin
    @ApiOperation(value = "获取新闻资讯详情")
    @RequestMapping(value = "/info", method = {RequestMethod.POST})
    public ApiRest<News> list(@RequestBody String id){
        return super.success(newsService.info(Long.parseLong(id)));
    }


    /**
     * 后端获取新闻资讯详情
     * @return
     */
    @LogInject(title = "后端获取新闻资讯详情")
    @CrossOrigin
    @ApiOperation(value = "后端获取新闻资讯详情")
    @RequestMapping(value = "/admin/info", method = {RequestMethod.POST})
    public ApiRest<News> list(@RequestBody NewsDelReqDTO newsDelReqDTO){
        return super.success(newsService.info(newsDelReqDTO.getNewsId()));
    }



    /**
     * 添加新闻资讯
     * @return
     */
    @LogInject(title = "添加新闻资讯")
    @CrossOrigin
    @ApiOperation(value = "添加新闻资讯")
    @RequestMapping(value = "/add", method = {RequestMethod.POST})
    public ApiRest<Boolean> add(@RequestBody News news){
        news.setCreateTime(new Date());
        if (newsService.save(news)){
            MemberDynamic memberDynamic = new MemberDynamic();
            memberDynamic.setMemberId(0L);
            memberDynamic.setCreateTime(new Date());
            memberDynamic.setTitle("我发布了一个新闻，快来阅读吧！");
            memberDynamic.setType(DynamicEnum.NEWS.getCode());
            memberDynamic.setItemId(news.getId());
            dynamicService.saveOrUpdate(memberDynamic);
        }else {
            return super.success(false);
        }
        return super.success(true);
    }


    /**
     * 编辑新闻资讯
     * @return
     */
    @LogInject(title = "编辑新闻资讯")
    @CrossOrigin
    @ApiOperation(value = "编辑新闻资讯")
    @RequestMapping(value = "/edit", method = {RequestMethod.POST})
    public ApiRest<Boolean> edit(@RequestBody News news){
        return super.success(newsService.saveOrUpdate(news));
    }


    /**
     * 删除新闻资讯
     * @return
     */
    @LogInject(title = "删除新闻资讯")
    @CrossOrigin
    @ApiOperation(value = "删除新闻资讯")
    @RequestMapping(value = "/delete", method = {RequestMethod.POST})
    public ApiRest<Boolean> edit(@RequestBody NewsDelReqDTO newsDelReqDTO){
        return super.success(newsService.removeById(newsDelReqDTO.getNewsId()));
    }
}
